package com.neuedu.shop.util;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;

/**
 * @Description: TODO(项目中的通用工具类)
 * @Author ${秦}
 * @Date 2021/4/13 16:04
 */
public class CommonUtil {
    private CommonUtil(){}
    private static Properties properties = new Properties();
    static {
        try {
            properties.load(CommonUtil.class.getClassLoader().getResourceAsStream("db.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static final String IMG_PATH = getProperty("filePath");
    public static String getProperty(String key){
        return properties.getProperty(key);
    }

    /*使用MD5加盐算法*/

    public static String stringToMD5(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(
                    plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有这个md5算法！");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }
    /*
    * 文件本地复制的方法
    * */
    public static void fileCopyToLocation(String srcPath,String destPath){
        File src = new File(srcPath);
        File dest = new File(destPath);
        //使用JDK提供的工具类完成
        try {
            Files.copy(src.toPath(),dest.toPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
